<?php

namespace App\Console\Commands;

use App\Models\Base\LogVipRank;
use App\Models\Base\ServerList;
use App\Service\ChannelService;
use App\Service\PlayerService;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Log;

class FetchVipUserRank extends CommandBase
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'fetch:vipRank';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'vip用户排名统计';

    /**
     * @var Model
     */
    protected $model;

    /**
     * Create a new command instance.
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $this->serverAllStat();
        $this->channleStat();
    }

    /**
     * 按全平台统计排名.
     */
    protected function serverAllStat()
    {
        $server_ids = ServerList::all()->pluck('server_id');
        foreach ($server_ids as $server_id) {
            setDB($server_id);
            $this->recordUserInfo($server_id, self::STAT_TYPE_PLATFORM);
        }
    }

    /**
     * 按渠道统计
     *
     * @return bool
     */
    protected function channleStat()
    {
        //获取所有渠道
        $channel_ids = ChannelService::getChannels();
        if ($channel_ids->isEmpty()) {
            Log::warning('There is no channel!');

            return false;
        }
        foreach ($channel_ids as $channel_id) {
            if (!$channel_id) {
                continue;
            }
            $server_ids = ServerList::all()->pluck('server_id');
            foreach ($server_ids as $server_id) {
                setDB($server_id);
                $this->recordUserInfo($server_id, self::STAT_TYPE_CHANNEL, $channel_id);
            }
        }
    }

    /**
     * 记录数据.
     *
     * @param null $channel_id
     *
     * @return void|bool
     */
    private function recordUserInfo($server_id, $type, $channel_id = null)
    {
        $player_data = PlayerService::getPayUserInfo($channel_id);
        if ($player_data->isEmpty()) {
            return false;
        }
        foreach ($player_data as $info) {
            $this->model = new LogVipRank();
            $logdb = LogVipRank::where('role_id', "{$info->id}")->where('type', $type)->first();
            if ($logdb) {
                $this->model = $logdb;
            }
            if ($server_id) {
                $this->model->server_id = $server_id;
            }
            if ($channel_id) {
                $this->model->channel_id = $channel_id;
                $this->model->type = self::STAT_TYPE_CHANNEL;
            } else {
                $this->model->type = self::STAT_TYPE_PLATFORM;
            }
            $this->model->role_id = (string) $info->id;
            $this->model->name = $info->name;
            $this->model->vip_level = getVIP($info->totaltreasure);
            $this->model->military = $info->fightingCapacity;
            $this->model->profession = $info->career;
            $this->model->amount = $info->totaltreasure / config('game.topUp');
            $last_pay_info = PlayerService::roleLastPayInfo($info->id);
            $this->model->last_pay_time = $last_pay_info ? $last_pay_info->created_at->toDateTimeString() : null;
            //充值天数
            $pay_days = PlayerService::rolePayDays($info->id);
            //注册天数
            $sign_days = ceil((time() - $info->registerTime) / 86400);
            //未充值天数
            $this->model->no_pay_days = $sign_days - $pay_days;
            //未登录天数
            $login_days = PlayerService::roleLoginDays($info->id);
            $this->model->no_login_days = $sign_days - $login_days;
            //最后登录记录
            $last_login_data = PlayerService::roleLastLogin($info->id);
            if ($last_login_data) {
                $this->model->last_login_time = date('Y-m-d H:i:s', $last_login_data->time);
            }
            $this->model->surplus_gold = $info->gold;
            $this->model->save();
        }
    }
}
